package cn.stylefeng.guns.modular.business.frequency.orderpickfree.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderpickfree.entity.OrderPickFree;
import cn.stylefeng.guns.modular.business.frequency.orderpickfree.mapper.OrderPickFreeMapper;
import cn.stylefeng.guns.modular.business.frequency.orderpickfree.model.param.OrderPickFreeParam;
import cn.stylefeng.guns.modular.business.frequency.orderpickfree.service.OrderPickFreeService;
import cn.stylefeng.guns.modular.business.frequency.orderpickfree.enums.OrderPickFreeExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 免费提短记录表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderPickFreeServiceImpl extends ServiceImpl<OrderPickFreeMapper, OrderPickFree> implements OrderPickFreeService {

    @Override
    public PageResult<OrderPickFree> page(OrderPickFreeParam orderPickFreeParam) {

        // 构造条件
        LambdaQueryWrapper<OrderPickFree> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderPickFree> list(OrderPickFreeParam orderPickFreeParam) {

        // 构造条件
        LambdaQueryWrapper<OrderPickFree> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderPickFreeParam orderPickFreeParam) {

        // 将dto转为实体
        OrderPickFree orderPickFree = new OrderPickFree();
        BeanUtil.copyProperties(orderPickFreeParam, orderPickFree);

        this.save(orderPickFree);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderPickFreeParam orderPickFreeParam) {
        this.removeById(orderPickFreeParam.getId());
    }

    @Override
    public void edit(OrderPickFreeParam orderPickFreeParam) {

        // 根据id查询实体
        OrderPickFree orderPickFree = this.queryOrderPickFree(orderPickFreeParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderPickFreeParam, orderPickFree);

        this.updateById(orderPickFree);
    }

    @Override
    public OrderPickFree detail(OrderPickFreeParam orderPickFreeParam) {
        return this.queryOrderPickFree(orderPickFreeParam);
    }



    /**
     * 获取免费提短记录表
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderPickFree queryOrderPickFree(OrderPickFreeParam orderPickFreeParam) {
        OrderPickFree orderPickFree = this.getById(orderPickFreeParam.getId());
        if (ObjectUtil.isEmpty(orderPickFree)) {
            throw new ServiceException(OrderPickFreeExceptionEnum.NOT_EXISTED);
        }
        return orderPickFree;
    }
}